//
//  OMOrderEvaluateDetailViewController.m
//  OMENGO_NEW
//
//  Created by 梁尚嘉 on 2017/5/17.
//  Copyright © 2017年 Kamfat. All rights reserved.
//

#import "OMOrderEvaluateDetailViewController.h"
#import "OMOrderEvaluateDetailInfoTableCell.h"
#import "OMOrderEvaluateDetailCommentTableCell.h"
#import "OMOrder.h"
#import "OMEvaluation.h"
@interface OMOrderEvaluateDetailViewController ()
@property (nonatomic, strong) NSArray *evaluates;
@property (nonatomic, strong) OMEvaluation *evaluation;
@end

@implementation OMOrderEvaluateDetailViewController

- (void)viewDidLoad {


    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupUI];
    [self startAnimating:YES];
    [self getEvaluateDetail];
}

- (void)reloadDataFromNetWork
{
    [self getEvaluateDetail];
}

- (void)getEvaluateDetail
{
    [KTHttpUtils GET:@"member/review/detail.jhtml"
          parameters:@{@"sn":self.order.sn}
            progress:nil
          completion:^(id responseObject, BOOL isSuccess, NSString *message) {
              if (isSuccess) {
                  [self startAnimating:NO];
                  self.evaluation = [OMEvaluation mj_objectWithKeyValues:responseObject[OMDatumKey]];
              }else {
                  [self showNetWorkError];
              }
    }];
}

- (void)setupUI
{
    self.title = @"查看评价";
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    [self.tableView registerClass:[OMOrderEvaluateDetailInfoTableCell class] forCellReuseIdentifier:[OMOrderEvaluateDetailInfoTableCell cellIdentifier]];
    [self.tableView registerClass:[OMOrderEvaluateDetailCommentTableCell class] forCellReuseIdentifier:[OMOrderEvaluateDetailCommentTableCell cellIdentifier]];
    self.evaluates = @[@"这是我见过的最最最最好的电商平台，真的，不骗呼哦！这是我见过的最最最最好的电商平台，真的，不骗你！这是我见过的最最最最好的电商平台，真的，不骗你!"];
}

- (void)setEvaluation:(OMEvaluation *)evaluation
{
    _evaluation = evaluation;
    self.evaluates = @[evaluation.content?:@""];
    [self.tableView reloadData];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (!self.evaluation) {
        return 0;
    }
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return self.evaluates.count;
    }
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        OMOrderEvaluateDetailInfoTableCell *cell = [OMOrderEvaluateDetailInfoTableCell tableViewCellWithTableView:tableView];
        [cell configureCellWithObj:self.evaluation];
        return cell;
    }else {
        OMOrderEvaluateDetailCommentTableCell *cell = [OMOrderEvaluateDetailCommentTableCell tableViewCellWithTableView:tableView];
        [cell configureCellWithObj:self.evaluates[indexPath.row]];
        return cell;
    }
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return[tableView fd_heightForCellWithIdentifier:[OMOrderEvaluateDetailInfoTableCell cellIdentifier] configuration:^(OMOrderEvaluateDetailInfoTableCell *cell) {
            [cell configureCellWithObj:self.evaluation];
        }];
    }
    return [OMOrderEvaluateDetailCommentTableCell cellHeightWithObject:self.evaluates[indexPath.row]];
}
@end

